@file:JvmName("MMKVUtils")

package com.yl.androidutilscenter.utils

import android.os.Parcelable
import com.tencent.mmkv.MMKV

/**
 * @author
 * @description:
 * @date :2024/3/14
 * @version 1.0.0
 */
object MMKVUtils {
    private val mmkv: MMKV by lazy { MMKV.defaultMMKV() }

    fun putValue(key: String, value: Any): MMKVUtils {
        when (value) {
            is String -> mmkv.encode(key, value)
            is Int -> mmkv.encode(key, value)
            is Long -> mmkv.encode(key, value)
            is Float -> mmkv.encode(key, value)
            is Double -> mmkv.encode(key, value)
            is Boolean -> mmkv.encode(key, value)
            is ByteArray -> mmkv.encode(key, value)
            is Parcelable -> mmkv.encode(key, value)
        }
        return this
    }


    fun getValue(key: String, default: String = ""): String {
        return mmkv.decodeString(key, default) ?: default
    }

    fun getValue(key: String, default: Int = -1): Int {
        return mmkv.decodeInt(key, default)
    }

    fun getValue(key: String, default: Long = -1L): Long {
        return mmkv.decodeLong(key, default)
    }

    fun getValue(key: String, default: Float = -1F): Float {
        return mmkv.decodeFloat(key, default)
    }

    fun getValue(key: String, default: Double = -1.0): Double {
        return mmkv.decodeDouble(key, default)
    }

    fun getValue(key: String, default: Boolean = false): Boolean {
        return mmkv.decodeBool(key, default)
    }

    fun getValue(key: String, default: ByteArray? = null): ByteArray? {
        return mmkv.decodeBytes(key, default)
    }

    fun <T : Parcelable> getValue(key: String, clazz: Class<T>, default: T?): T? {
        return mmkv.decodeParcelable(key, clazz, default)
    }

    fun clearAll() {
        mmkv.clearAll()
    }

}